package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {


    @Override
    public ResponseResult list(SensitiveDto dto) {
        //1.校验参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //校验page和size
        dto.checkParam();

        //2.构建条件,执行查询 分页方法(父类ServiceImpl里有page方法)
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdSensitive::getSensitives,dto.getName());
        }

        //分页对象
        IPage<AdSensitive> ipage = new Page<>(dto.getPage(),dto.getSize());
        IPage<AdSensitive> pageResult = super.page(ipage, wrapper);

        //3.返回结果
       ResponseResult responseResult= new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal(),pageResult.getRecords());
       return responseResult;
    }


    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.参数校验
        if(adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行保存
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        //返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1.参数校验
        if(adSensitive.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.执行修改
        updateById(adSensitive);
        //save(adSensitive);
        //3.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //1.参数校验
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查看敏感词是否存在  查看前先找出ById
        AdSensitive sensitive = getById(id);
        if(sensitive==null){
return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //执行删除
        removeById(id);
        //返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
